package ogb;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests the (generally nonexistent) evasive capabilities of DeaconBlues.
 * 
 * @author Branden Ogata
 *
 */
public class TestDeaconBluesEvasion extends RobotTestBed {
  private double time0 = 0.0;
  private double time1 = 0.0;
  private double longestTime = 0.0;
  private double lastEnergy = 100.0;
  
  /**
   * Specifies Walls and DeaconBlues as the robots involved in this test.
   * 
   * @return  The names of the robots in this match.
   * 
   */
  
  @Override
  public String getRobotNames() {
    return "sample.Walls,ogb.DeaconBlues";
  }

  /**
   * Returns the number of rounds in this match.
   * 
   * @return  The number of rounds in this test.
   * 
   */
  
  @Override
  public int getNumRounds() {
    return 8;
  }
  
  /**
   * Each turn, check if DeaconBlues has taken damage and record the times if so.
   *
   * @param e The event that the game sends.
   * 
   */
  
  @Override
  public void onTurnEnded(TurnEndedEvent e) {
    // Current information about DeaconBlues
    IRobotSnapshot self = e.getTurnSnapshot().getRobots()[1];
    
    double robotEnergy = self.getEnergy();
    
    // Note that this means does not account for loss of energy due to firing cannon
    // or running into walls.
    if (robotEnergy < lastEnergy) { 
      time0 = time1;
      time1 = e.getTurnSnapshot().getTurn();
      
      if (time1 - time0 > longestTime) {
        longestTime = time1 - time0;
      }
    }   
  }
  
  /**
   * When battle is concluded, examine how good DeaconBlues is at evading.
   * 
   * @param e The event that the game sends.
   */
  
  @Override
  public void onBattleCompleted(BattleCompletedEvent e) {
    assertTrue("Check evasion", longestTime > 8);
  }  
}
